package com.kye.express.business.personal_my_bill.Bill.list;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.personal_my_bill.Bill.detail.BillDetailActivity;
import com.kye.express.databinding.ItemBillBinding;
import com.kye.model.bean.bill.BillListBean;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;

public class BillViewBinder extends ItemViewBinder<BillListBean.BillBean, BillViewBinder.ViewHolder> {

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_bill, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull BillListBean.BillBean bill) {
        holder.bind(bill, getAdapter().getItems());
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private final ItemBillBinding mBind;
        private List<?> mItems;

        ViewHolder(View itemView) {
            super(itemView);
            mBind = ItemBillBinding.bind(itemView);
            RxView.clicks(mBind.getRoot())
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            BillDetailActivity.startActivity(itemView.getContext(), getAdapterPosition(), new ArrayList<>(mItems));
                        }
                    });
        }

        public void bind(BillListBean.BillBean bill, List<?> items) {
            mItems = items;
            mBind.setBean(bill);
            mBind.executePendingBindings();
        }
    }
}
